package com.candoo.root.security.domain;

import lombok.Data;

import java.io.Serializable;

/**
 * @ClassName:  ResultJson   
 * @Description:TODO(描述这个类的作用)   
 * @author: i@mxpio.com
 * @date:   2020年4月29日 下午5:26:12    
 * @param <T>  
 * @Copyright:  https://gitee.com/i_mxpio
 */
@Data
public class ResultJson<T> implements Serializable{

    private static final long serialVersionUID = 783015033603078674L;
    private int code;
    private String msg;
    private T data;

    public static <T> ResultJson<T> ok() {
        return ok("");
    }

    @SuppressWarnings("unchecked")
	public static <T> ResultJson<T> ok(Object o) {
        return new ResultJson<T>(ResultCode.SUCCESS,(T) o);
    }

    public static <T> ResultJson<T> failure(ResultCode code) {
        return failure(code, "");
    }

    @SuppressWarnings("unchecked")
	public static <T> ResultJson<T> failure(ResultCode code, Object o) {
        return new ResultJson<T>(code,(T) o);
    }

    public ResultJson (ResultCode resultCode) {
        setResultCode(resultCode);
    }

    public ResultJson (ResultCode resultCode,T data) {
        setResultCode(resultCode);
        this.data = data;
    }

    public void setResultCode(ResultCode resultCode) {
        this.code = resultCode.getCode();
        this.msg = resultCode.getMsg();
    }

    @Override
    public String toString() {
        return "{" +
                "\"code\":" + code +
                ", \"msg\":\"" + msg + '\"' +
                ", \"data\":\"" + data + '\"'+
                '}';
    }
}
